<?php 
	 // 连接数据库 创建PDO 对象 重点是dsn 的配置
	 // 
	$dsn = "mysql:dbname=bookstore;host=localhost";
	$user = "root";
	$password = "Wyj961012";

	try {
		$dbh = new PDO($dsn, $user, $password);
		// 设置错误使用异常的模式
		$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		// 关闭自动提交
		// $dbh->setAttribute(PDO::ATTR_AUTOCOMMIT, 0);
	} catch (PDOException $e) {
		echo 'Connection failed: ' . $e->getMessage();
		exit;
	}
	try {
		$stmt = $dbh -> prepare("select * from books");
		$stmt -> execute();
		// 在处理结果前可以设置结果的模式 ,以下代码使用fetch() 和 fetchAll() 都是用这边设置的数组格式
		$stmt -> setFetchMode(PDO::FETCH_ASSOC);


		// echo "<pre>";
		// print_r($stmt -> fetchAll(PDO::FETCH_NUM));
		// echo "</pre>";

		/*
		echo '<table border="1" width=800 align="center">';
		while (list($id, $bookname ) = $row = $stmt -> fetch(PDO::FETCH_NUM)) {
			echo "<tr>";
			echo "<td>".$id."</td>";
			echo "<td>".$bookname."</td>";
			echo "</tr>";
		}
		
		echo "</table>";
		// 获取结果中的总行数，或者影响行数
		// $count = $stmt -> rowCount();  
		// 获取最后一个自动增长的ID
		// $dbh -> lastInsertId();
		*/
	} catch (PDOException $e) {
		echo $e -> getMessage();
	}



	
	
	/*
		PDO::setAttribute
	// echo $dbh->getAttribute(PDO::ATTR_SERVER_VERSION)."<br>";
	// echo $dbh->getAttribute(PDO::ATTR_SERVER_INFO)."<br>";
	// echo $dbh->getAttribute(PDO::ATTR_ERRMODE);
	 */
	


	/**
	 *	查询语句
	 * 	1. exex() 用于处理非结果集 insert update delete create ... 影响行 用这个
	 * 	能够返回影响的函数 
	 * 	2. query() 用于处理有结果集的语句 select desc show  影响结果集 用这个
	 * 	返回来的是 PDOStatement 对象，再通过这个类的方法，获取结果，也可以直接foreach遍历（不推荐）
	 * 	
		try{
		$stemt = $dbh -> query("select * from books");
		foreach ($stemt as $arr ) {
			print_r($arr);
			echo "<br>";
		}
		} catch (PDOException $e){
			echo $e -> getMessage();
		}
	
	*/



/*
	// 事务处理，当其中一个语句发生错误时，就会全部暂停并且回滚操作
	try{
		$dbh->beginTransaction();

		$price = 100;
		$affected_rows = $dbh -> exec("update books set price ={$price} where id = 32");
		if($affected_rows > 0) {
			echo "success";
		} else {
			echo "failed";
			throw new PDOException("Error Processing Request1");
		}
		$affected_rows = $dbh -> exec("update books set price ={$price} where id = 500");
		if($affected_rows > 0) {
			echo "success";
		} else {
			echo "failed";
			throw new PDOException("Error Processing Request2");
		}
		$dbh->commit();

	} catch (PDOException $e) {
		echo $e->getMessage();
		$dbh->rollBack();
	}


	// 执行完毕后 恢复自动提交
	$dbh -> setAttribute(PDO::ATTR_AUTOCOMMIT, 1);
	*/


	/*
	// 预处理
	try {
		$sth = $dbh -> prepare("insert into books (bookname, publisher, author, price, ptime, pic, detail) values (:bookname, :publisher, :author, :price, :ptime, :pic, :detail)");
		$sth -> bindParam("bookname", $name, PDO::PARAM_STR); //第三个参数 手动指定 类型
		$sth -> bindParam("publisher", $name);
		$sth -> bindParam("author", $name);
		$sth -> bindParam("price", $name);
		$sth -> bindParam("ptime", $name);
		$sth -> bindParam("pic", $name);
		$sth -> bindParam("detail", $name);

		$name = "0000000022222000";

		$sth->execute();
		$sth->execute();
		$sth->execute();
		$sth->execute();
		$sth->execute();
		$sth->execute();

		// 更便捷的方式 ，在执行 execute 的同时，又赋值
		// $sth->execute(array("name" =>"a", "xx" => "xx"));
		// $sth->execute(array("name", "xx"));
		// $sth->execute($_GET);
		// 

	} catch (PDOException $e) {
		echo $e -> getMessage();
	}
	*/




 ?>